	$(document).ready(function () {
				var myDate = new Date();    
				 var strDate = myDate.toLocaleDateString();  
				$("#date_input").val(strDate);
				
				//加载数据表格
				loadTable();
	});
	
	//清除筛选条件
	function clearsj(){
		$("#customerName").textbox("clear");
		$("#customerPhone").textbox("clear");
		$("#customerHybh").textbox("clear");
		location.reload();
		
	}
	function loadTable()
	{
		$('#chargeDebt_datagrid').datagrid({
			cls: "theme-datagrid",
			singleSelect: true,
			rownumbers: true, //显示序号
			fitColumns:true,
			onClickRow: onClickRow,
		    method: 'post',
		    onClickRow: onClickRow,
			columns:[[
			  		{field:'ids',title:'orderids',hidden:true},
			  		{field:'ordernumber',title:'订单编号',width:100, align:'center',
			  			formatter: function(value,row,index){
			  				var ids = row.ids;
							return '<a href="#" style="color: blue;" class="button-edit" onclick="lookInfo(\'' + ids + '\')">' + value + '</a>';
						}},
			  		{field:'arrears',title:'欠款金额（元）',width:80, align:'center'},
			  		{field:'newreceivable',title:'应收款（元）',width:80, align:'center',
			  			formatter: function(value,row,index){
			  				var arr=row.arrears;
			  				return  row.newreceivable=arr;
						}
			  		},
			  		{field:'newproceeds',title:'实收款（元）',width:100, align:'center',
			  			editor:{type:'numberspinner',
			  				options:{
			  					min:1,
			  					onChange : function(n){
				  					getArrears(n);
				  				}
			  				}
			  		}},
			  		{field:'newarrears',title:'欠款（元）',width:80, align:'center'},
			  		{field:'operate',title:'操作',width:80, align:'center',formatter: function(value,row,index){
		  				var ids = row.orderids;
						return '<a href="#"  class="button-edit button-success  l-btn l-btn-small" onclick="repay(\'' + index + '\')"><span class="l-btn-left" ><span class="l-btn-text">还款</span></span></a>';
						
			  		}}
			      ]]
		})
		
	}
	/*数据表格行编辑*/
	function onClickRow(index){
		$('#chargeDebt_datagrid').datagrid('selectRow', index)
			.datagrid('beginEdit', index);
	}
	function getArrears(n)
	{	
		var row = $('#chargeDebt_datagrid').datagrid('getSelected');
		var index=$('#chargeDebt_datagrid').datagrid('getRowIndex', row);
		var d=parseInt(row.newreceivable)-parseInt(n);
		$('#chargeDebt_datagrid').datagrid('updateRow', {
	        index: index,
	        row: {
	        	newproceeds:n,
	        	newarrears:d
	        }
	    });
		$('#chargeDebt_datagrid').datagrid('beginEdit', index);
	}
	
	//获取顾客信息
	function searchCus()
	{	
		var reData=getCusInfo();
		if(reData.msg==200)
		{
			//清空表格
			var rows = $('#chargeDebt_datagrid').datagrid('getRows');
            for (var i = 0; i <rows.length; i++) {
                var index = $('#chargeDebt_datagrid').datagrid('getRowIndex', rows[i]);
                $('#chargeDebt_datagrid').datagrid('deleteRow', index);
            }
            $('#chargeDebt_datagrid').datagrid('loadData', { total: 0, rows: [] });
            var url='/bchargeDebt/getQkOrder';
			var data={"customerIds":reData.customer.ids};
			var result = common_ajax.ajaxFunc(url,data,"json");
            if(result.msg==200)
            {
            	$('#chargeDebt_datagrid').datagrid({
                	data:result.psoList
                });
            }else
            {
            	$.alert(parent.$, result.msg);
            }
            
		}
	}
		
	
	
	$(document).keydown(function(event){ 
		if(event.keyCode == 13){
			getCusInfo();
			$('#chargeDebt_datagrid').datagrid("endEdit", editIndex1);
		} 
	}); 
	//点击产品编号，出现产品信息
	function lookInfo(obj){
		loadDialog(null,null, '订单详情', 'bchargeDebt/showOrder?orderIds='+obj);
	}
	//还款
	function repay(index){
		 $("#allData").html("");
		 $('#chargeDebt_datagrid').datagrid('endEdit', index);
		 var row = $('#chargeDebt_datagrid').datagrid('getRows')[index];
		 
		 $("#allData").append("<input type='hidden' name='collectArrears.orderIds' value='"+row.ids+"'/>");
		 $("#allData").append("<input type='hidden' name='collectArrears.orderArrears' value='"+row.arrears+"'/>");
		 $("#allData").append("<input type='hidden' name='collectArrears.receivable' value='"+row.newreceivable+"'/>");
		 $("#allData").append("<input type='hidden' name='collectArrears.proceeds' value='"+row.newproceeds+"'/>");
		 $("#allData").append("<input type='hidden' name='collectArrears.arrears' value='"+row.newarrears+"'/>");
		 	var url='/bchargeDebt/repayment';
			var data=$('#arrears_form').serialize();
			var reData = common_ajax.ajaxFunc(url,data);
			if(reData == "200")
			{	
//				 $("#chargeDebt_datagrid").datagrid('reload');
				 $.slideMsg("还款成功", parent.$);
				 parent.refresh();
//				 getCusInfo();
				
//				 if(row.newarrears == 0)
//				 {
//					 $('#chargeDebt_datagrid').datagrid('deleteRow', index);
//				 }
				
			}else
			{
				$.alert(parent.$, reData);
			}
	}
	var submitForm = function($dialog, $grid, $tree, $pjq) {
		submitNow($dialog, $grid, $tree, $pjq);
	};
	
	/*数据表格行编辑*/
	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#chargeDebt_datagrid').datagrid('validateRow', editIndex)){
			$('#chargeDebt_datagrid').datagrid('endEdit', editIndex);
				editIndex = undefined;
				return true;
		} else {
			return false;
		}
	}
	function onClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#chargeDebt_datagrid').datagrid('selectRow', index)
					.datagrid('beginEdit', index);
					editIndex = index;
			} else {
				$('#chargeDebt_datagrid').datagrid('selectRow', editIndex);
			}
		}
	}